<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct(){
		parent::__construct();
		date_default_timezone_set("PRC");
		$this->load->library('session');
		$this->load->helper('url');
	}
	public function index()
	{
		$this->load->library('pagination');
		$config=array(
			'base_url'=>site_url('c=main&m=index'),
			'total_rows'=>$this->db->count_all('hope'),
			'per_page'=>10,
			'page_query_string'=>TRUE,
			'query_string_segment'=>'page',
			'full_tag_open'=>'<div class="page">',
			'full_tag_close'=>'</div>',
			'first_link'=>'首页',
			'last_link'=>'尾页'
		);
		$this->pagination->initialize($config);
		$page=$this->pagination->create_links();

		$result=$this->db
						->select('*')
							->from('hope')
								->order_by('hope_id','DESC')
								->limit($config['per_page'],($this->pagination->cur_page)*$config['per_page'])
									->get()->result();
		// var_dump($result);
		$this->load->view('index',array('result'=>$result,'page'=>$page));
	}
	public function hope(){
		$post=$this->input->post();
		$result=false;
		$response=array(
				'status'=>0,
				'msg'=>'没有提交数据'
			);
		if($post){
			$captcha=$this->session->captcha;
			if(strtolower($post['captchacode'])!=strtolower($captcha)){
				$response=array(
					'status'=>0,
					'msg'=>'验证码错误'
				);
			}else if($post['name']==''){
				$response=array(
					'status'=>0,
					'msg'=>'署名不能为空'
				);
			}else if($post['content']==''){
				$response=array(
					'status'=>0,
					'msg'=>'内容不能为空'
				);
			}else if(mb_strlen($post['content'])>200){
				$response=array(
					'status'=>0,
					'msg'=>'内容不能超过200字'
				);
			}else if($post['icon']==''){
				$response=array(
					'status'=>0,
					'msg'=>'图标不能为空'
				);
			}else{
				unset($_SESSION['captcha']);
				$data=array(
					'name'=>$post['name'],
					'content'=>$post['content'],
					'time'=>time(),
					'icon'=>$post['icon'],
				);
				$result=$this->db->insert('hope',$data);
				if($result){
					$response=array(
						'status'=>1,
						'msg'=>'许愿成功'
					);
				}else{
					$response=array(
						'status'=>0,
						'msg'=>'数据提交失败'
					);
				}
			}
		}
		echo json_encode($response);
	}
	public function captcha(){
		$vals = array(
		    'word'      => '',
		    'img_path'  => './captcha/',
		    'img_url'   => 'http://'.$_SERVER['HTTP_HOST'].'/'.dirname($_SERVER['SCRIPT_NAME']).'/captcha/',
		    'font_path' => BASEPATH.'/fonts/texb.ttf',
		    'img_width' => '100',
		    'img_height'    => 30,
		    'expiration'    => 7200,
		    'word_length'   => 4,
		    'font_size' => 16,
		    'img_id'    => 'Imageid',
		    'pool'      => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',

		    // White background and border, black text and red grid
		    'colors'    => array(
		        'background' => array(255, 255, 255),
		        'border' => array(255, 255, 255),
		        'text' => array(0, 0, 0),
		        'grid' => array(255, 40, 40)
		    )
		);
		$this->load->helper("captcha");
		$cap = create_captcha($vals);
		$this->session->set_userdata(array('captcha'=>$cap['word']));
		readfile($vals['img_url'].$cap['filename']);
	}
}
